package com.platform.service.impl;
import com.github.pagehelper.PageInfo;
import com.platform.annotation.SysLog;
import com.platform.base.BaseService;
import com.platform.common.ServerResponse;
import com.platform.service.IBigFieldService;
import com.platform.util.MapperUtil;
import com.platform.util.NumberUtil;
import com.platform.util.PropertiesUtil;
import com.platform.util.StringUtil;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 大字段服务层
 */
@Service("iBigFieldService")
public class BigFieldServiceImpl extends BaseService implements IBigFieldService {

    /**
     * 同步耗材价格数据至solr
     *
     * @return
     */
    @SysLog("同步耗材价格数据至solr")
    public ServerResponse<String> synTextSolr(Map paramMap) {
        int countNums = 0;
        try {
            List<Map> resultList = bigFieldMapper.selectByParamMap(new HashMap());
            boolean flag = true;
            if (null != resultList && 0 < resultList.size()) {
                HttpSolrServer solrServer = new HttpSolrServer(PropertiesUtil.getProperty("solr.url"));
                SolrInputDocument doc = new SolrInputDocument();
                for (int i = 0; i < resultList.size(); i++) {
                    String fieldname = StringUtil.valueOfString(resultList.get(i).get("fieldname"));
                    int eid = NumberUtil.safeToNumber(resultList.get(i).get("eid"));
                    if(flag){
                        doc = new SolrInputDocument();
                        flag = false;
                        doc.addField("id",UUID.randomUUID());
                        doc.addField("eid",eid);
                        doc.addField("productname",resultList.get(i).get("productname"));//大字段类型：产品名称
                        doc.addField("manufacture",resultList.get(i).get("manufacture"));//生产厂家
                        doc.addField("price",NumberUtil.safeToDouble(resultList.get(i).get("price")));//价格
                        doc.addField("datahome",resultList.get(i).get("datahome"));//数据来源
                        doc.addField("datatime",resultList.get(i).get("datatime"));//数据时间
                        doc.addField("registration_number",resultList.get(i).get("registration_number"));//注册证名称
                    }
                    if(fieldname.equals("specifications")){
                        doc.addField("specifications",resultList.get(i).get("bigtext"));//规格 大字段
                    }else if(fieldname.equals("model")){
                        doc.addField("model",resultList.get(i).get("bigtext"));//型号 大字段
                    }
                    if((i+1)%2 == 0 &&
                            StringUtil.isNotEmpty(StringUtil
                                    .valueOfString(doc.get("specifications"))) &&
                            StringUtil.isNotEmpty(StringUtil
                                    .valueOfString(doc.get("model")))){
                        //由于数据量太大 二个大字段连表分类查出会卡死数据库，所以排序后用java判断
                        solrServer.add(doc);
                        flag = true;
                    }
                    countNums++;
                }
                solrServer.commit();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ServerResponse.createBySuccessMessage("同步成功" + countNums + "条数据");
    }
}
